package bat.ke.qq.com.tx.example;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SpringTransactionExample {

    private static String url = "jdbc:mysql://localhost:3306/bat_tran?" +
            "characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
    private static String user = "root";
    private static String password = "root";

    public static void main(String[] args) {
        // 获取数据源
        final DataSource ds = new DriverManagerDataSource(url, user, password);
        // 编程式事务
        final TransactionTemplate template = new TransactionTemplate();
        // 设置事务管理器
        template.setTransactionManager(new DataSourceTransactionManager(ds));
        template.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                Connection conn = DataSourceUtils.getConnection(ds);
                Object savePoint = null;
                try {
                    {
                        // 插入
                        PreparedStatement prepare = conn.
                                prepareStatement("insert into t_account (username,balance)" +
                                        " values (?,?)");
                        prepare.setString(1, "fox");
                        prepare.setInt(2, 100);
                        prepare.executeUpdate();
                    }
                    //int i=1/0;

                    // 设置保存点
                    savePoint = status.createSavepoint();
                    {
                        // 插入
                        PreparedStatement prepare = conn.
                                prepareStatement("insert into t_account " +
                                        "(username,balance) values (?,?)");
                        prepare.setString(1, "monkey");
                        prepare.setInt(2, 300);
                        prepare.executeUpdate();
                    }

                    {
                        // 更新
                        PreparedStatement prepare = conn.
                                prepareStatement("update t_account set balance=0 where username=?");
                        prepare.setString(1, "fox");
                        Assert.isTrue(prepare.executeUpdate() > 0, "");


                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    System.out.println("更新失败");
                    if (savePoint != null) {
                        status.rollbackToSavepoint(savePoint);
                    } else {
                        status.setRollbackOnly();
                    }
                }
                return null;
            }
        });
    }
}